package com.sc.test.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Toast;

import com.sc.test.bean.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Shen Chao on 2017-12-27-027.
 */

public class Util {
    private StringBuffer sb;

    public static void openNewActivity(Context context, Class cls, String result) {
        Intent intent = new Intent(context, cls);
        //传递数据
        Bundle bundle = new Bundle();
        bundle.putString("result", result);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void openNewActivityInt(Context context, Class cls, int layout) {
        Intent intent = new Intent(context, cls);
        //传递数据
        Bundle bundle = new Bundle();
        bundle.putInt("layout", layout);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public void addString(String s) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append(s);
        sb.append("\n");
    }

    public String finishString() {
        return sb.toString();
    }

    public void clearString() {
        if ((sb != null && sb.length() > 0)) {
            sb.delete(0, sb.length());
        }
    }

    public static Student strToStudent(String s) {
        Student student = new Student();
        student.setName(s);
        return student;
    }

    public static List<Student.Course> strToList(String s) {
        List<Student.Course> courseList = new ArrayList<>();
        Student.Course course1 = new Student.Course();
        Student.Course course2 = new Student.Course();
        Student.Course course3 = new Student.Course();
        course1.setName("yuwen");
        course2.setName("shuxue");
        course3.setName("English");
        if ("zhangsan".equals(s)) {
            courseList.add(course1);
            courseList.add(course3);
            return courseList;
        }
        if ("lisi".equals(s)) {
            courseList.add(course1);
            courseList.add(course2);
            return courseList;
        }
        if ("wangwu".equals(s)) {
            courseList.add(course1);
            courseList.add(course2);
            courseList.add(course3);
            return courseList;
        }
        return null;
    }

    public static void toast(Context context, String message) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static int dpToPx(int dp) {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    /**
     * px转dp
     *
     * @param pxVal
     * @return
     */
    public static float px2dp(float pxVal) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (pxVal / scale);
    }
}